package com.dhcc.sds.standard.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.standard.dao.SdsStandDicMapper;
import com.dhcc.sds.standard.dao.SdsStandSourceMapper;
import com.dhcc.sds.standard.entity.SdsStandDicEntity;
import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sds.standard.dao.SdsStandDicsubMapper;
import com.dhcc.sds.standard.service.ISdsStandDicsubService;
import com.dhcc.sds.standard.entity.SdsStandDicsubEntity;
import org.springframework.util.StringUtils;

/**
 * 项目标准化字典子项表数据服务层接口
 * 
 * @ClassName: SdsStandDicsubServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:53
 */
@Service("sdsStandDicsubService")
public class SdsStandDicsubServiceImpl extends BaseServiceImpl<SdsStandDicsubMapper, SdsStandDicsubEntity> implements ISdsStandDicsubService {

	@Autowired
	private SdsStandDicMapper standDicMapper;

	@Autowired
	private SdsStandDicsubMapper standDicsubMapper;

	@Autowired
	private SdsStandSourceMapper sourceMapper;

	@Autowired
	private ISdsStandDicService standDicService;

	@Autowired
	private ISdsStandSourceService standSourceService;

	@Override
	public List<SdsStandDicsubEntity> page(Page<SdsStandDicsubEntity> page, Map<String, Object> map) {
		List<SdsStandDicsubEntity> page1 = this.baseMapper.page(page, map);
		for(SdsStandDicsubEntity dicsubEntity : page1){
			SdsStandDicEntity standDicEntity = standDicMapper.selectById(dicsubEntity.getDicId());
			dicsubEntity.setDicCatDesc(standDicEntity.getName());

		}

		return page1;
	}
	@Override
	public List<SdsStandDicsubEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public List<SdsStandDicsubEntity> pageByDrug(Page<SdsStandDicsubEntity> page, Map<String, Object> params) {

		List<SdsStandDicsubEntity> standDicsubEntities = new ArrayList<>();

		//1.查询数据源包含药品
		List<SdsStandSourceEntity> sdsStandSourceEntities = sourceMapper.selectList(
				new EntityWrapper<SdsStandSourceEntity>()
						.like("source_desc", "药物")
		);


		List<SdsStandDicEntity> standDicEntities = new ArrayList<>();
		//2.根据数据源查询标准化字典
		for(SdsStandSourceEntity sourceEntity : sdsStandSourceEntities){
			standDicEntities.addAll(standDicService.listBySource(sourceEntity.getSourceDesc()));
		}


		//3.根据药品分类查询药品名称
		for(SdsStandDicEntity standDicEntity : standDicEntities){

			params.put("dic_id", standDicEntity.getId());
			standDicsubEntities = standDicsubMapper.page(page, params);
			//standDicsubEntities.addAll(page1);
		}
		return standDicsubEntities;
	}

	@Override
	public List<SdsStandDicsubEntity> pageByDic(Page<SdsStandDicsubEntity> page, Map<String, Object> params, List<SdsStandDicEntity> standDicEntitys) {
		List<Long> ids = new ArrayList<>();
		for (SdsStandDicEntity standDicEntity : standDicEntitys){
			ids.add(standDicEntity.getId());
		}
		Object dicId = params.get("dicId");
		if ( (dicId == null) || ("".equals(dicId)) ){
			return this.baseMapper.pageByDic(page, params, ids);

		}else{
			return this.baseMapper.page(page, params);
		}

	}

    @Override
    public List<SdsStandDicsubEntity> listByDic(List<SdsStandDicEntity> standDicEntitys) {
        List<Long> ids = new ArrayList<>();
        for (SdsStandDicEntity standDicEntity : standDicEntitys){
            ids.add(standDicEntity.getId());
        }
        return this.baseMapper.listByDic(ids);
    }

	@Override
	public List<ZTreeNode> getEntityTree(String sourceDesc) {
		if ("all".equals(sourceDesc)){
			//查询全部
			List<ZTreeNode> tree = this.baseMapper.treeAll();
			tree.add(ZTreeNode.createRoot("用药信息"));
			return tree;
		}else if (Objects.equals("用药信息", sourceDesc)){
			//查询抗菌药物、非抗菌药物、病历信息、处置操作
			List<String> list = new ArrayList<>();
			list.add("抗菌药物");
			list.add("非抗菌药物");
			//list.add("电子病历");
			list.add("处置操作");
			List<ZTreeNode> tree = this.baseMapper.treeBySources(list);
			tree.add(ZTreeNode.createRoot(sourceDesc));
			return tree;
		} else if (Objects.equals("关键字信息", sourceDesc)){
			//查询常用短语、非抗菌药物
			//List<ZTreeNode> tree = this.baseMapper.treeKeyInfo();
			List<String> list = new ArrayList<>();
			list.add("常用短语");
			//list.add("非抗菌药物");
			List<ZTreeNode> tree = this.baseMapper.treeBySources(list);
			tree.add(ZTreeNode.createRoot(sourceDesc));
			return tree;
		} else {
			//单个数据源
			SdsStandSourceEntity sourceByDesc = standSourceService.getSourceByDesc(sourceDesc);
			String sourceCode = sourceByDesc.getSourceCode();
			List<ZTreeNode> tree = this.baseMapper.tree(sourceCode);
			tree.add(ZTreeNode.createRoot(sourceDesc));
			return tree;
		}

	}

	@Override
	public SdsStandDicsubEntity getByDicAndDicsubDesc(Long dicId, String dicsubDesc) {
		SdsStandDicsubEntity dicsubEntity = this.baseMapper.selectByDicAndDicsubDesc(dicId, dicsubDesc);
		if (CommonUtil.isEmpty(dicsubEntity)){
			SdsStandDicsubEntity dicsubEntity1 = new SdsStandDicsubEntity();
			dicsubEntity1.setDicId(dicId);
			dicsubEntity1.setDicDesc(dicsubDesc);
			this.baseMapper.insert(dicsubEntity1);
			return dicsubEntity1;
		}else {
			return dicsubEntity;
		}
	}


}